﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Samples_TestStockFormIn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        initGrid();
        if (!IsPostBack)
        {
            divMain.Visible = true;
            divPostback.Visible = false;
            
        }
        else
        {
            divMain.Visible = !true;
            divPostback.Visible = !false;
            
        }
    }
    public void initGrid()
    {
        this.dotNetFlexGrid1.InitConfig(
            new string[]{
                "title=入库单商品清单",//标题
                "striped=true",//是否显示行交替色
                "selectedonclick=true",//是否点击行自动选中checkbox
                "usepager=false",//使用分页器
                "showcheckbox=false",//显示复选框
                "height=200",//高度，可为auto或具体px值
                "width=580"//宽度，可为auto或具体px值
            },
            new dotNetFlexGrid.FieldConfig[]{
                new dotNetFlexGrid.FieldConfig("ProductId","编号",60,false,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("ProductName","商品名",100,false,dotNetFlexGrid.FieldConfigAlign.Center),
                new dotNetFlexGrid.FieldConfig("ProductNumber","数量",50,false,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("ProductPrice","单价",50,false,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("ProductCode","批号",60,false,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("ProductState","状态",50,false,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("Upd","",50,false,dotNetFlexGrid.FieldConfigAlign.Right,false,false,true,"<a href=\'javascript:editProd(\"[@ProductId]\");\'>编辑</a>"),
                new dotNetFlexGrid.FieldConfig("Del","",50,false,dotNetFlexGrid.FieldConfigAlign.Right,false,false,true,"<a href=\'javascript:delProd(\"[@ProductId]\");\'>删除</a>"),
                new dotNetFlexGrid.FieldConfig("OpType","编辑类型",60,false,dotNetFlexGrid.FieldConfigAlign.Right,false, true,true,"dbdata")
            }
        );
        if (Request.QueryString["action"] != null && Request.QueryString["action"] == "update")
        {
            dotNetFlexGrid1.DataHandler = new dotNetFlexGrid.DataHandlerDelegate(DotNetFlexGrid1DataHandler);
            string formId = Request.QueryString["formId"];
            dotNetFlexGrid1.DefaultSearch.Add("formId", formId);
        }
    }
    public dotNetFlexGrid.DataHandlerResult DotNetFlexGrid1DataHandler(dotNetFlexGrid.DataHandlerParams p)
    {
        dotNetFlexGrid.DataHandlerResult result = new dotNetFlexGrid.DataHandlerResult();
        DataSet ds = Samples_TestStockFormList.DsStockFormData;
        DataRow[] drs = ds.Tables["StockFormListDetail"].Select("FormId='" + p.defaultSearch["formId"] + "'");
        DataTable dt = ds.Tables["StockFormListDetail"].Clone();
        foreach (DataRow dr in drs)
            dt.ImportRow(dr);

        result.page = p.page;//设定当前返回的页号
        result.total = dt.Rows.Count;//总计的数据条数，
        result.table = dt;//调用演示的数据生成函数产生模拟数据
        dotNetFlexGrid.FieldFormatorHandle proc=delegate(DataRow dr)
        {
            switch (dr["ProductState"].ToString().ToLower())
            {
                case "0":
                    return "<span style='color:red'>禁用</span>";
                case "1":
                    return "<span style='color:Blue'>下架</span>";
                case "2":
                    return "<span style='color:Gray'>正常销售</span>";
                case "3":
                    return "已售空";
            }
            return "";
        };
        dotNetFlexGrid.FieldFormatorHandle proc1 = delegate(DataRow dr)
        {
            return "￥" + dr["ProductPrice"].ToString();
        };
        result.FieldFormator.Register("ProductState", proc);
            result.FieldFormator.Register("ProductPrice",proc1);

        return result;
    }
    public static DataTable DemoMemoryTable(int num)
    {
        DataTable result = new DataTable("js_flexigrid");
        result.Columns.Add("ProductId", System.Type.GetType("System.String"));
        result.Columns.Add("ProductName", System.Type.GetType("System.String"));
        result.Columns.Add("ProductNumber", System.Type.GetType("System.Int32"));
        result.Columns.Add("ProductPrice", System.Type.GetType("System.Single"));
        result.Columns.Add("ProductCode", System.Type.GetType("System.String"));
        result.Columns.Add("ProductState", System.Type.GetType("System.Int32"));


        for (int i = 0; i < num ; i++)
        {
            DataRow dr = result.NewRow();

            dr["ProductId"] = i.ToString();
            dr["ProductName"] = "测试商品_" + i.ToString();
            dr["ProductNumber"] = new Random(i).Next(1, 100);
            dr["ProductPrice"] = (float)new Random(i).Next(1, 1000)/10;
            dr["ProductCode"] = "测试批号_" + i.ToString();
            dr["ProductState"] = new Random(i).Next(0,4);

            result.Rows.Add(dr);
        }
        return result;
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        DataTable result = new DataTable("StockFormIn");
        result.Columns.Add("ProductId", System.Type.GetType("System.String"));
        result.Columns.Add("ProductName", System.Type.GetType("System.String"));
        result.Columns.Add("ProductNumber", System.Type.GetType("System.Int32"));
        result.Columns.Add("ProductPrice", System.Type.GetType("System.Single"));
        result.Columns.Add("ProductCode", System.Type.GetType("System.String"));
        result.Columns.Add("ProductState", System.Type.GetType("System.String"));
        result.Columns.Add("OpType", System.Type.GetType("System.String"));


        Dictionary<string, Dictionary<string, string>> listData = dotNetFlexGrid1.ParseGetGridJsonData(txtListData.Value);
        
        foreach(string key in listData.Keys)
        {
            DataRow dr = result.NewRow();
            dr["ProductId"] = listData[key]["ProductId"];
            dr["ProductName"] = listData[key]["ProductName"];
            dr["ProductNumber"] = int.Parse(listData[key]["ProductNumber"]);
            dr["ProductPrice"] = Single.Parse(listData[key]["ProductPrice"].Substring(1,listData[key]["ProductPrice"].Length-1));
            dr["ProductCode"] = listData[key]["ProductCode"];
            dr["ProductState"] = listData[key]["ProductState"];
            dr["OpType"] = listData[key]["OpType"];

            result.Rows.Add(dr);
        }
        this.Repeater1.DataSource = result;
        this.Repeater1.DataBind();

    }
    
}
